Skip to content

Conversation

pavoljuhas
Copy link
Collaborator

Add return-type to public functions, mostly tests part 4

No change in the effective code. A batch of ~50 files.

Modified files pass ruff check --select=ANN201

Partially implements #4393

No change in the effective code.  A batch of ~50 files.

Modified files pass  ruff check --select=ANN201

Partially implements quantumlib#4393
@pavoljuhas pavoljuhas requested review from a team and vtomole as code owners September 10, 2025 06:21
@pavoljuhas pavoljuhas requested a review from viathor September 10, 2025 06:21
@github-actions github-actions bot added the size: XL lines changed >1000 label Sep 10, 2025
Copy link

codecov bot commented Sep 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.37%. Comparing base (6816824) to head (0709e48).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7645      +/-   ##
==========================================
- Coverage   99.37%   99.37%   -0.01%     
==========================================
  Files        1078     1078              
  Lines       96166    96212      +46     
==========================================
+ Hits        95563    95608      +45     
- Misses        603      604       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few drive-by comments.

self._memo: dict[Any, dict] = {}

def default(self, o):
def default(self, o) -> Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that maybe we could be more specific. Perhaps something like

def default(self, o) -> bool | list[Any] | dict[str, Any]:
    ...

might work. Maybe one could be even more specific?

Note 1: It is useful to know that if the return type isn't a bool then it's something one can iterate over, and that if it's not a list, then we can iterate over its keys and rely on them being strings.

Note 2: A better opportunity to engineer neat narrow type annotations for these functions is unlikely to materialize in future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done in 96e4cbe.

def measurement_key_obj(val: Any, default: Any = RaiseTypeErrorIfNotProvided):
def measurement_key_obj(
val: Any, default: TDefault = RaiseTypeErrorIfNotProvided
) -> cirq.MeasurementKey | TDefault:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function never returns the type TDefault of default, so it would be nice not to have to deal with the alternative at the callsites. It looks like type inference failure in

if default is not RaiseTypeErrorIfNotProvided:
    return default

Is there a way to explain to mypy better what's actually going on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can use the @overload decorator to distinguish calls without the default when the return type is certain. When default is provided, the output can be still either of MeasurementKey | TDefault. Thanks for the suggestion, done in 0709e48.



def with_measurement_key_mapping(val: Any, key_map: Mapping[str, str]):
def with_measurement_key_mapping(val: Any, key_map: Mapping[str, str]) -> Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do better than Any here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We can use a generic type T for the val type and expect the same type to be returned. Done in 0709e48.

Specify types that can be actually returned rather than Any.
Annotate with `@overload` for the return type is known when the `default`
is not provided.
Copy link
Contributor

@mhucka mhucka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pavoljuhas pavoljuhas added this pull request to the merge queue Sep 17, 2025
Merged via the queue into quantumlib:main with commit 1fa73c7 Sep 17, 2025
35 checks passed
@pavoljuhas pavoljuhas deleted the add-missing-return-type-4 branch September 17, 2025 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: XL lines changed >1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants